﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.PUBC;
using Mang24k99.Business.PUBC;

namespace Mang24k99.Web.PUBC
{
    public partial class BoardType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel1.Visible = false;
                BindGridView();
            }
        }

        public void BindGridView()
        {
            MKtPubcProduct[] productlist = (MKtPubcProduct[])BKtPubcProduct.SelectAll();
            grdList.DataSource = productlist;
            grdList.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidatePage()==false)
            {
                return;
            }
            if (txtBordNO.Text.Trim()=="")//新增
            {
                if (BKtPubcProduct.NameValidate(txtName.Text.Trim()) == false)
                {
                    string strMsg = "调查表名称不能重复！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                if (BKtPubcProduct.AliasValidate(txtAlias.Text.Trim())==false)
                {
                    string strMsg = "调查表简称不能重复！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                MKtPubcProduct product = new MKtPubcProduct();
                product.BoardNO = (int)MKtPubcProduct.GetMaxNO() + 1;
                product.BoardName = txtName.Text.Trim();
                product.BoardAlias = txtAlias.Text.Trim();
                product.IsValidate = 1;
                product.CycNum = 0;
                product.InputTime = DateTime.Now;
                try
                {
                    product.Create();
                    string strMsg = "添加成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {

                }
            }
            else
            {
                MKtPubcProduct product = BKtPubcProduct.Find(Convert.ToInt32(txtBordNO.Text));
                if (txtName.Text.Trim()!=product.BoardName)
                {
                    if (BKtPubcProduct.NameValidate(txtName.Text.Trim()) == false)
                    {
                        string strMsg = "调查表名称不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                }
                if (txtAlias.Text.Trim()!=product.BoardAlias)
                {
                    if (BKtPubcProduct.AliasValidate(txtAlias.Text.Trim()) == false)
                    {
                        string strMsg = "调查表简称不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                }
                product.BoardName = txtName.Text.Trim();
                product.BoardAlias = txtAlias.Text.Trim();
                product.ModifyTime = DateTime.Now;
                try
                {
                    product.Update();
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (txtBordNO.Text!="")  //编辑状态
            {
                MKtPubcProduct product = BKtPubcProduct.Find(Convert.ToInt32(txtBordNO.Text));
                txtName.Text = product.BoardName;
                txtAlias.Text = product.BoardAlias;
            }
            else
            {
                txtAlias.Text = "";
                txtName.Text = "";
            }
        }
        /// <summary>
        /// 清空页面
        /// </summary>
        public void ClearPage()
        {
            txtAlias.Text = "";
            txtName.Text = "";
            txtBordNO.Text = "";
            btnSave.Text = "保存";
            Panel1.Visible = false;
        }
        public  bool ValidatePage()
        {
            if (txtName.Text.Trim()=="")
            {
                string strMsg = "调查表名称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtAlias.Text.Trim()=="")
            {
                string strMsg = "调查表简称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int boardno = Convert.ToInt32(e.CommandArgument.ToString());
            MKtPubcProduct product = BKtPubcProduct.Find(boardno);
            if (e.CommandName == "Modi")
            {
                txtBordNO.Text = product.BoardNO.ToString();
                txtName.Text = product.BoardName;
                txtAlias.Text = product.BoardAlias;
                btnSave.Text = "更新";
                Panel1.Visible = true;
            }
            if (e.CommandName=="Del")
            {
                try
                {
                    product.Delete();
                    string strMsg = "删除成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            ClearPage();
        }
    }
}
